<template>
  <div style="width: 100%;" class="card-area">
    <layout-content topTitle="查询" bottomTitle="领取列表">
      <!-- 搜索区域 -->
      <template v-slot:topCard>
        <search-view ref="searchView" @search="search"/>
      </template>
      <template v-slot:extraOperation>
      </template>
      <!-- 表格区域 -->
      <template v-slot:bottomCard>
        <!-- <p class="desc">{{ SERVER_YEAR }}年{{ SERVER_MONTH }}月&emsp;第{{ SERVER_WEEK }}周</p> -->
        <a-table
          ref="TableInfo"
          :columns="columns"
          rowKey="id"
          :dataSource="dataSource"
          :loading="loading"
          @change="handleTableChange"
          :pagination="pagination"
        >
            <template slot="status" slot-scope="text">
            <span>{{ statusMap[text] }}</span>
          </template>
          <template slot="operation" slot-scope="text, record">
            <a-button type="link" @click="info(record)" class="icon">详情</a-button>
          </template>
        </a-table>
        <!-- </a-card> -->
      </template>
    </layout-content>
    <!-- 查看 -->
    <info-record
      :visible="infoVisible"
      @close="infoVisible = false"
      :initRecord="selectedRecord"
      :apiURL="apiURL" 
    >
    </info-record>
  </div>
</template>

<script>
import columns from './columns';
import modal from '~/tool/modal';
import apiURL from './requestURL';
// import addRecord from './add';
import infoRecord from './info';
import layoutContent from '@/components/tool/layout-content';
import searchView from './search-view';
// import { PLAN_STATUS_KEY, PLAN_STATUS_VALUE } from 'utils/business-enum';
// import { mapState } from 'vuex';

export default {
  name: 'mainPage',
  components: { layoutContent, modal, searchView, infoRecord},
  data() {
    return {
      // 驳回理由
      // rejectReason: '',
      // 驳回理由modal可见性
      // rejectReasonVisible: false,
      // 状态渲染
      // PLAN_STATUS_VALUE,
      // PLAN_STATUS_KEY,
      // 添加或编辑
      addOrEdit: 0,
      // 组件渲染的key
      addKey: new Date().getTime(),
      // 表头
      columns,
      // 接口url
      apiURL,
      // 组件的可见性
      addVisible: false,
      infoVisible: false,
      // 当前选中的记录
      selectedRecord: {},

      dataSource: [],
      sortedInfo: null,
      paginationInfo: null,
      pagination: {
        size: 'small',
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false,
      statusMap: Object.freeze({
        '0': '待使用',
        '1': '已使用'
      }),
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.search();
  },
  methods: {
    /**
     * @desc: 由search-view组件触发的导出文件函数
     * @param {object} 子组件传入的用户输入的信息
     * @author: youzi
     */
    exportFile(params) {
      this.$export(apiURL.exportRecords, {
        ...params
      });
    },
    handleOperationClick(key, record = {}) {
      this.addOrEdit = key === 'add' ? 0 : 1;
      this.selectedRecord = record;
      this[`addKey`]++;
      this[`addVisible`] = true;
    },
    /**
     * @desc: 新增成功时的回调函数，隐藏新增组件，提示，查询
     * @param {type}
     * @author: youzi
     */
    onAddSuccess() {
      this.addVisible = false;
      this.$message.success('操作成功');
      this.search();
      // this.$refs['searchView'].handleSearch();
    },
    /**
     * @desc: 查看组件，传入一条记录
     * @param {object} record 当前选择的这条记录
     * @author: youzi
     */
    info(record) {
      this.selectedRecord = record;
      this.infoVisible = true;
    },
    /**
     * @desc: 删除记录
     * @param {object} 一条记录
     * @author: youzi
     */
    onDelete(record) {
      let that = this;
      this.$confirm({
        title: '确定删除吗?',
        content: '删除将不能恢复哦！',
        centered: true,
        onOk() {
          that.$delete(apiURL.deleteRecord+ record.id).then(() => {
            that.$message.success('删除成功');
            that.search();
          });
        },
        onCancel() {}
      });
    },
    /**
     * @desc: feature：当reset的时候，是否需要重置分页信息，目前先注释了
     * @author: youzi
     */
    /* reset() {
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent;
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent;
        this.paginationInfo.pageSize = this.pagination.defaultPageSize;
      }
      // 重置列排序规则
      this.sortedInfo = null;
    }, */
    /**
     * @desc: 分页信息改变时的回调函数
     * @param 分页信息，过滤信息，排序信息
     * @author: youzi
     */
    handleTableChange(pagination, filters, sorter) {
      // 将这两个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination;
      this.sortedInfo = sorter;
      /* this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order
      }); */
      // this.$refs['searchView'].handleSearch();
      this.search();
    },
    /**
     * @desc: 调用fetch的入口函数，先拿到当前列的排序和过滤信息
     * @author: youzi
     */
    search(params = {}) {
      let { sortedInfo } = this;
      let sortField, sortOrder;
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field;
        sortOrder = sortedInfo.order;
      }
      this.fetch({
        ...params,
        sortField: sortField,
        sortOrder: sortOrder
      });
    },
    /**
     * @desc: 查询记录的函数
     * @param {object} params 包含查询条件，分页等信息
     * @author: youzi
     */
    fetch(params = {}) {
      this.loading = true;
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current;
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize;
        params.count = this.paginationInfo.pageSize;
        params.currentPage = this.paginationInfo.current;
      } else {
        // 如果分页信息为空，则设置为默认值
        params.count = this.pagination.defaultPageSize;
        params.currentPage = this.pagination.defaultCurrent;
      }
      this.$post(apiURL.getRecords, {
        ...params
      })
        .then(r => {
          let data = r.data.data;
          let pagination = { ...this.pagination };
          pagination.total = data.total;
          this.dataSource = data.rows;
          this.pagination = pagination;
          this.loading = false;
        })
        .catch(e => {
          console.error(e);
          this.loading = false;
        });
    }
  }
};
</script>

<style lang="less" scoped>
/* @import '../../../../static/less/Common';
.card-area-bottom {
  margin-top: 1rem;
}
.card-area {
  .card-area-top,
  .card-area-bottom {
    border-radius: 6px;
  }
  .card-title {
    font-size: 1rem !important;
    font-weight: bold;
    color: #393d47;
    border-left: 4px solid #408bf4;
    padding-left: 15px;
  }
} */
/* .hover-content {
  width: 20rem;
} */
.reject-reason-content {
  text-align: center;
}
.divider {
  margin: 0 4px;
}
.desc {
  font-size: 20px !important;
  color: rgba(0, 0, 0, 0.6);
}
.icon {
  /* svg {
    height: 20px;
    width: 20px;
  } */
  padding: 0;
  border: none;
  color: #1890ff;
}
.card-area {
  .extra-btn {
    width: 86px;
  }
}
.search-btn {
  margin-top: 8px;
}
.border-span {
  width: 2px;
  /* border: 1px solid rgb(207, 215, 222); */
  background-color: #cfd7de;
  height: 16px;
  margin-right: 2px;
  display: inline-block;
}
</style>
